package com.test.zhangxin.jianpin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.test.zhangxin.jianpin.adapter.RvHorAdapter;
import com.test.zhangxin.jianpin.adapter.RvVerAdapter;
import com.test.zhangxin.jianpin.bean.Template;
import com.test.zhangxin.jianpin.presenter.TemplatePresenter;
import com.test.zhangxin.jianpin.view.TemplateView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements TemplateView {

    private RecyclerView mRvVer;
    private RecyclerView mRvHor;
    private int[] mImgs;
    private List<Template> mTatolList;//所有数据
    private List<Template> mTypeList;//
    private List<Template> mTemplateList;//给RecyclerView展示的数据
    private RvVerAdapter mRvVerAdapter;//垂直滑动的列表
    private RvHorAdapter mHorAdapter;//水平滑动的列表
    private int start;
    private int end;//数据长度
    private int mOffset = 10;//每次要添加的数据长度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TemplatePresenter presenter = new TemplatePresenter(this);
        presenter.getTemplateList();
        initUI();
        initData();
        initEvent();
    }

    private void initUI() {
        mRvVer = (RecyclerView) findViewById(R.id.rv_ver);
        mRvHor = (RecyclerView) findViewById(R.id.rv_hor);
    }

    private void initData() {
        mTatolList = new ArrayList<>();
        mTemplateList = new ArrayList<>();
        mTypeList = new ArrayList<>();
        resetIndex();
        mImgs = new int[]{R.drawable.logo_wall, R.drawable.logo_note,
                R.drawable.logo_cover, R.drawable.logo_splice,
                R.drawable.logo_businesscard, R.drawable.logo_postcard, R.drawable.logo_long,
                R.drawable.logo_favourite, R.drawable.logo_history};
        //垂直列表
        mRvVerAdapter = new RvVerAdapter(this, mTemplateList);
        //防止数据错乱
        mRvVerAdapter.setHasStableIds(true);
        mRvVer.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));
        mRvVer.setItemAnimator(null);
        mRvVer.setAdapter(mRvVerAdapter);
        mRvVer.addItemDecoration(new SpaceItemDecoration(30));//分隔线
        //水平列表
        mHorAdapter = new RvHorAdapter(this, mImgs);
        //防止数据错乱
        mHorAdapter.setHasStableIds(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRvHor.setLayoutManager(linearLayoutManager);
        mRvHor.setAdapter(mHorAdapter);
    }

    private void initEvent() {
        mHorAdapter.setOnItemClickListener(new RvHorAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Log.e("zx", "position::" + position);
                mTypeList.clear();
                //将mTypeList换成对应类型的数据
                for (int i = 0; i < mTatolList.size(); i++) {
                    Template template = mTatolList.get(i);
                    if (template.getRestype_id() == position) {
                        mTypeList.add(template);
                    } else {
                        Toast.makeText(getApplicationContext(), "没有数据哦", Toast.LENGTH_SHORT);
                    }
                }
                mTemplateList.clear();
                resetIndex();
                end = end < mTypeList.size() ? end : mTypeList.size();
                mTemplateList.addAll(mTypeList.subList(start, end));
                mRvVerAdapter.notifyDataSetChanged();
            }
        });
        mRvVer.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                int size = mTypeList.size();
                if (!mRvVer.canScrollVertically(1) && start != size) {
                    Log.e("zx", "end");
                    start = end;
                    end = end + mOffset >= size ? size : end + mOffset;
                    Log.e("zx", "start::" + start);
                    Log.e("zx", "end::" + end);
                    mTemplateList.addAll(mTypeList.subList(start, end));
                    //TODO:刷新列表
                    mRvVerAdapter.notifyItemInserted(start);
                }
            }
        });
    }

    private void resetIndex() {
        //重置起始位置
        start = 0;
        end = 10;
    }

    @Override
    public void getDataSuccessful(List<Template> templateList) {
        if (templateList.size() == 0)
            return;
        mTatolList.clear();
        for (int i = 0; i < templateList.size(); i++) {
            Template template = templateList.get(i);
            if ("vertical".equals(template.getRes_arr().get(0).getProportion())) {//图片有两种类型,这里只要一种的情况
                mTatolList.add(template);
                if (template.getRestype_id() == 0)
                    mTypeList.add(template);
            }
        }
        end = end < mTypeList.size() ? end : mTypeList.size();
        Log.e("zx", "size::" + mTypeList.size());
        mTemplateList.addAll(mTypeList.subList(start, end));
        mRvVerAdapter.notifyDataSetChanged();
    }

    @Override
    public void getDataFailed() {
        Log.e("zx", "获取数据失败");
    }
}
